<template>
	<view>
		<!-- 轮播图 -->
		<hm-banner :bannerLength="bannerLength" :bannerList="bannerList" :index="index"></hm-banner>

		<!-- 菜单 -->
		<hm-menu></hm-menu>
		

		<view style="border-bottom: 10rpx solid #EFEFF4;">
			<view @tap="recgoods">
				<view class="taday-title">
					今日推荐
					<image src="/static/remao_shop/img/right1.jpg"></image>
				</view>
			</view>
			<view class="list-warpss">
				<view v-for="(item, index) in todayList" :key="index" @tap="toRushdetails" class="todaylist-list" :data-index="index"
				 :data-img="item.thumb_img" :data-price="item.price">
					<image :src="item.thumb_img" mode="widthFix"></image>
					<!-- <load-img :scroll-top="scrollTop" :image-src="item.thumb_img" :view-height="height" open-transition="true"></load-img> -->
					<view class="todayListName">{{item.goods_name}}</view>
					<view style="color: #E41F53">￥{{item.price}}</view>
				</view>
				<view class="zanwu" v-if="todayList.length == 0">暂无商品</view>
			</view>
		</view>
		<!-- 好店推荐 -->
		<view v-if="hiddenShop != 1" style="border-bottom: 10rpx solid #EFEFF4;">
			<navigator url="../../packageB/shopList/shopList">
				<view class="taday-title">
					好店精选
					<image src="/static/remao_shop/img/right1.jpg"></image>
				</view>
			</navigator>
			<view class="list-warpss">
				<view class="list shop">
					<view v-for="(item, index) in shopList" :key="index" class="shopimg" :data-index="index" :data-img="item.store_logo"
					 @tap="toShop">
						<image :src="item.store_logo" width="100%"></image>
						<!-- <load-img scroll-top="{{scrollTop}}" image-src="{{item.store_logo}}" view-height="{{height}}" open-transition="{{true}}"></load-img>  -->
						<text>{{item.store_name}}</text>
					</view>
				</view>
			</view>
		</view>
		<view>
			<view @tap="goodshot">
				<view class="taday-title">
					爆款商品
					<image src="/static/remao_shop/img/right1.jpg"></image>
				</view>
			</view>
			<view class="list-warpss">
				<view v-for="(item, index) in hotList" :key="index" @tap="toRushdetailsHot" class="todaylist-list" :data-index="index"
				 :data-img="item.thumb_img">
					<image :src="item.thumb_img" mode="widthFix"></image>
					<!-- <load-img scroll-top="{{scrollTop}}" mode="widthFix"  image-src="{{item.thumb_img}}" view-height="{{height}}" open-transition="{{true}}"></load-img>      -->
					<view class="todayListName">{{item.goods_name}}</view>
					<view style="color: #E41F53">￥{{item.price}}</view>
				</view>
				<view class="zanwu" v-if="hotList.length == 0">暂无商品</view>
			</view>
		</view>
		<!--<view class="gg" wx:if="{{gg}}">
  <view class="contents">
    <image src="../../img/right1.jpg"></image>
      <button bindtap="ljqg">立即抢购</button>
  <view class="fang"></view>
  <view class="gb" bindtap="intgb"><image src="../../img/gggb.png"></image></view>
  </view>
</view>-->
		<form report-submit="true" @submit="submit">
			<button formType="submit"></button>
		</form>
	</view>
</template>

<script>
	import api from "../../../api";
	import utils from "../../../utils/utils";
	var app = getApp(); // const debounce = (fn, delay=300) => {
	//   let ctx,
	//       args;
	//   let timer = null;
	//   const later = function(){
	//     fn.apply(ctx, args)
	//     timer = null;
	//   }
	//   return function(){
	//     ctx = this
	//     args = arguments;
	//     if(timer){
	//       clearTimeout(timer)
	//       timer = null
	//     }
	//   }
	//   timer = setTimeout(later, delay )  
	// }
	import loadImg from "../../component/loadimg/loadimg";
	import hmBanner from "./components/hm-banner.vue"
	import hmMenu from "./components/hm-menu.vue"

	export default {
		data() {
			return {
				id: 1,
				indicatorDots: true,
				autoplay: false,
				interval: 5000,
				duration: 500,
				index: 0,
				bannerLength: false,
				bannerList: [],
				// 今日推荐
				todayList: [],
				// 热门商品
				hotList: [],
				shopList: [],
				shopID: [],
				shopName: [],
				shopLogo: [],
				menu: [],
				loginModelHidden: !0,
				uid: 0,
				hiddenShop: 0,
				gg: false,
				height: '',
				list: [],
				scrollTop: 0,
				userId: ""
			};
		},

		components: {
			loadImg,
			hmBanner,
			hmMenu
		},
		props: {},

		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad: function(e) {
			console.log(e);
			this.getList();
			this.today();
			this.getshop();
			this.hot();
			this.getIsOpen();
			this.menuFun();
			wx.getStorage({
				key: 'userId',
				success: function(res) {
					console.log(res.data);
				}
			});
			wx.getSystemInfo({
				// 获取页面可视区域的高度
				success: res => {
					console.log(res);
					this.setData({
						height: res.windowHeight
					});
				}
			});
		},

		/**
		 * 生命周期函数--监听页面初次渲染完成
		 */
		onReady: function() {
			this.setData({
				gg: true
			});
		},

		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow: function() {
			this.getIsOpen();
		},

		/**
		 * 生命周期函数--监听页面隐藏
		 */
		onHide: function() {},

		/**
		 * 生命周期函数--监听页面卸载
		 */
		onUnload: function() {},

		/**
		 * 页面相关事件处理函数--监听用户下拉动作
		 */
		onPullDownRefresh: function() {
			this.getList();
			this.today();
			this.getshop();
			this.hot();
			this.getIsOpen();
			this.we7login();
			wx.stopPullDownRefresh();
		},

		/**
		 * 页面上拉触底事件的处理函数
		 */
		onReachBottom: function() {},

		/**
		 * 用户点击右上角分享
		 */
		onShareAppMessage: function(e) {
			let uid = wx.getStorageSync('userId');
			let data = '';
			let nickName = '';
			let title = '';
			let path = `/remao_shop/pages/index/index?uid=${uid}`;
			let imageUrl = '';

			let shareCallBack = () => { //分享成功后做的事情
				//
				//
			}; //此处调用封装好的分享代码


			return utils.onShareAppMessage(title, path, shareCallBack);
		},
		methods: {
			//拼团路由
			topintuan() {
				wx.navigateTo({
					url: '../../packageA/groupbook/groupbook'
				});
			},

			// 轮播图
			searchList: function() {
				wx.navigateTo({
					url: '../../packageA/searchList/searchList'
				});
			},
			//搜索页面
			search: function() {
				wx.navigateTo({
					url: '../../packageB/search/search'
				});
			},
			//优选页面
			choose: function() {
				wx.navigateTo({
					url: '../../packageC/choose/choose'
				});
			},

			//轮播图跳转


			getIsOpen: function() {
				app.globalData.request({
					url: api.gzSea.open,
					data: {},
					header: {
						'content-type': 'application/x-www-form-urlencoded' // 默认值

					},
					method: 'POST',
					success: res => {
						console.log(res);
						this.setData({
							hiddenShop: res.data
						});
						console.log(this.hiddenShop);
					}
				});
			},
			//首页轮播图
			getList: function() {
				let that = this;
				app.globalData.request({
					url: api.default.banner_list,
					data: {
						id: 1 // id: 1

					},
					header: {
						'content-type': 'application/x-www-form-urlencoded' // 默认值

					},
					method: 'POST',
					success: res => {
						console.log(res);
						that.setData({
							bannerList: res
						});
						var length = res.length;

						if (length == 1) {
							that.setData({
								bannerLength: false
							});
						} else {
							that.setData({
								bannerLength: true
							});
						}
					},
					fail: function(err) {},
					complete: function(res) {}
				});
			},
			menuFun: function() {
				var that = this;
				app.globalData.request({
					//url: "/app/index.php?i=18&t=1&v=2.9&brand=1&from=wxapp&c=entry&a=wxapp&do=XcxMenu&&m=hotmallstore&sign=4b1eec37473b06e8bc31716c48edccfc",
					//url: "https://we7.puddingz.com/app/index.php?i=18&t=1&v=2.9&brand=1&from=wxapp&c=entry&a=wxapp&do=XcxMenu&&m=hotmallstore&sign=4b1eec37473b06e8bc31716c48edccfc",//另外一个接口的菜单
					header: {
						'content-type': 'application/x-www-form-urlencoded' // 默认值

					},
					method: 'POST',
					data: {
						i: 18,
						t: 1,
						v: 2.9,
						brand: 1,
						from: 'wxapp',
						c: 'entry',
						a: 'wxapp',
						do: 'XcxMenu',
						m: 'hotmallstore',
						sign: '4b1eec37473b06e8bc31716c48edccfc'
					},
					success: res => {
						console.log(res);
						that.setData({
							menu: res
						});
					}
				});
			},
			//今日推荐
			today: function() {
				let that = this;
				app.globalData.request({
					url: api.fyz.indexToday,
					header: {
						'content-type': 'application/x-www-form-urlencoded' // 默认值

					},
					method: 'POST',
					data: {
						type: 2,
						goods_type: 1,
						page: 1
					},
					success: res => {
						console.log(res);
						that.todayList = [];
						let list = []; //   for (let a = 0; a < 4; a++) {
						//     var item = res.list[a]
						//     list = list.concat(item)
						//   }

						that.setData({
							todayList: res.list
						}); // console.log(that.data.todayList)
					}
				});
			},
			// 热门推荐
			hot: function() {
				let that = this;
				app.globalData.request({
					url: api.fyz.indexToday,
					header: {
						'content-type': 'application/x-www-form-urlencoded' // 默认值

					},
					method: 'POST',
					data: {
						type: 1,
						goods_type: 1,
						page: 1
					},
					success: res => {
						console.log(res);
						that.hotList = [];
						let list = []; //   for (let a = 0; a < 4; a++) {
						//     var item = res.list[a]
						//     list = list.concat(item)
						//   }

						this.setData({
							hotList: res.list
						}); // console.log(that.data.hotList)
					}
				});
			},
			// 今日推荐路由
			toRushdetails: function(e) {
				let that = this;
				console.log(e);
				let index = e.currentTarget.dataset.index;
				let img = e.currentTarget.dataset.img;
				wx.navigateTo({
					url: '../../packageA/rushdetails/rushdetails?goods_id=' + that.todayList[index].goods_id + '&img=' + img +
						'&ismiaosha=0'
				});
			},
			// 爆款商品路由
			toRushdetailsHot: function(e) {
				let that = this;
				console.log(e);
				let index = e.currentTarget.dataset.index;
				let img = e.currentTarget.dataset.img;
				wx.navigateTo({
					url: '../../packageA/rushdetails/rushdetails?goods_id=' + that.hotList[index].goods_id + '&img=' + img +
						'&ismiaosha=0'
				});
			},

			//店铺推荐
			getshop() {
				let that = this;
				console.log(app.globalData);
				app.globalData.request({
					url: api.default.selected_Shops,
					header: {
						'content-type': 'application/x-www-form-urlencoded' // 默认值

					},
					method: 'POST',
					data: {
						page: '1'
					},
					success: res => {
						console.log(res.data);
						let id = [];
						let name = [];
						let logo = [];
						let list = [];

						if (res.data.length > 0) {
							if (res.data.length > 3) {
								for (let i = 0; i < 3; i++) {
									id = id.concat(res.data[i].id);
									name = name.concat(res.data[i].store_name);
									logo = logo.concat(res.data[i].store_logo);
									list[i] = res.data[i];
								}
							} else {
								for (let i = 0; i < res.data.length; i++) {
									id = id.concat(res.data[i].id);
									name = name.concat(res.data[i].store_name);
									logo = logo.concat(res.data[i].store_logo);
									list[i] = res.data[i];
								}
							}

							that.setData({
								shopList: list,
								shopID: id,
								shopName: name,
								shopLogo: logo
							});
							console.log(logo);
						}
					}
				}); // wx.request({
				// 	url: 'http://hotmall.shiliucrm.com/api/store/selected_store',
				// 	header: {
				// 		'content-type': 'application/x-www-form-urlencoded' // 默认值
				// 	},
				// 	method: 'POST',
				// 	data: {
				// 		page: '1'
				// 	},
				// 	success: (res) => {
				// 		console.log(res)
				// 		let id = [],
				// 		  name = [],
				// 		  logo = [],
				// 		  list = [];
				// 		for (let i = 0; i < 3; i++) {
				// 		  id = id.concat(res.data.data[i].id)
				// 		  name = name.concat(res.data.data[i].store_name)
				// 		  logo = logo.concat(res.data.data[i].store_logo)
				// 		  list[i] = res.data.data[i]
				// 		}
				// 		that.setData({
				// 		  shopList: list,
				// 		  shopID: id,
				// 		  shopName: name,
				// 		  shopLogo: logo
				// 		})
				// 		console.log(logo)
				// 	}
				// })
			},

			//店铺路由
			toShop(e) {
				// console.log(e)
				var l = e.currentTarget.dataset.index; // console.log(l)

				var id = this.shopID[l];
				var name = this.shopName[l];
				var logo = this.shopLogo[l];
				wx.navigateTo({
					url: '../../packageA/goodShop/goodShop?id=' + id + '&name=' + name + '&logo=' + logo
				});
			},

			// 微擎登录
			we7login(e) {
				if (this.uid == 0) {
					var t = this,
						a = wx.getStorageSync("userInfo"); // console.log('aaaaa')
					// console.log(a)
					// console.log(wx.getStorageSync("userInfo"))

					a && 0 != a.memberInfo.uid && "" != a.memberInfo ? t.setData({
						userId: a.memberInfo.uid
					}) : wx.getSetting({
						success: function(e) {
							// console.log(e)
							// console.log(e.authSetting["scope.userInfo"])
							false == e.authSetting["scope.userInfo"] ? wx.showModal({
								title: "提示",
								content: "允许小程序获取您的用户信息后才可查看线索哦",
								showCancel: !1,
								success: function(e) {
									// console.log(e.authSetting["scope.userInfo"])
									e.confirm && wx.openSetting({
										success: function(e) {
											1 == e.authSetting["scope.userInfo"] && (t.setData({
												loginModelHidden: !1
											}), wx.removeStorageSync("userInfo"));
										}
									});
								}
							}) : (wx.removeStorageSync("userInfo"), t.setData({
								loginModelHidden: !1
							})); // console.log(wx.removeStorageSync("userInfo"))
							// console.log(t.setData)
						}
					});
				}
			},

			// 获取用户信息
			updateUserInfo: function(e) {
				var t = this;
				app.globalData.util.getUserInfo(function(e) {
					// console.log(e)
					// t.login();
					// console.log(userInfo.sessionid)
					e = wx.getStorageSync("userInfo"); // console.log('狗')

					t.setData({
						userId: e.memberInfo.uid,
						loginModelHidden: !0
					});
				}, e.detail);
			},
			intgb: function() {
				this.setData({
					gg: false
				});
			},
			ljqg: function() {
				wx.navigateTo({
					url: ''
				}), this.setData({
					gg: false
				});
			},
			//跳转今日推荐
			recgoods: function() {
				wx.navigateTo({
					url: '../../packageA/recgoods/recgoods?type=' + 2
				});
			},
			//跳转热门商品
			goodshot: function() {
				wx.navigateTo({
					url: '../../packageA/recgoods/recgoods?type=' + 1 // url='../../packageA/goodshot/goodshot'

				});
			},

			//收集formID
			submit(e) {
				console.log(e);
				var a = e.detail.formId;
				wx.getStorage({
					key: 'userId',
					success: function(aaa) {
						console.log(aaa.data);
						var open = aaa.data;
						console.log(open);

						if (a == 'the formId is a mock one') {
							wx.showToast({
								title: '请用手机测试'
							});
						} else {
							console.log(a);
							wx.showToast({
								title: '1111'
							}); //   app.request({
							//     url: api.gzSea.test, //仅为示例，并非真实的接口地址
							//     data: {
							//       form_id: a,
							//       user_id: open
							//     },
							//     method: 'POST',
							//     header: {
							//       'content-type': 'application/x-www-form-urlencoded' // 默认值
							//     },
							//     success(res) {
							//       console.log(res)
							//     }
							//   })
						}
					}
				});
			}

		}
	};
</script>
<style>
	@import "./index.css";
</style>
